import React, { Component } from 'react';
import propTypes from 'prop-types';
import Form from '@wanhu/antd-legacy/lib/form';
// import Input from '@wanhu/antd-legacy/lib/input';
import { Icon, Input, message } from 'antd';
import { diseaseInfo, func } from '@wanhu/business/lib/health-record/prop-types';
import api from '../../../../../../api/api';
import './index.less';
const { TextArea } = Input;
const FormItem = Form.Item;
const debounce = function (fn, wait = 1000) {
    let time = null;
    return function () {
        const self = this
        if (time) {
            clearTimeout(time);
            time = null;
        }
        time = setTimeout(function () {
            console.log('thisthis', self);
            fn.apply(this);
        },wait)
    }
}
export default class DiseaseInfoEdit extends Component {
    state = {
        allergies: '',
        allergiesEdit: true,
        allergiesSaveLoading:false
    }

    // @toastErrorCaptureThrows
    async componentDidMount() {
        // this.fun()
        // rebuild start
        this.getSlowSickData()
    }

    // 新的方法，下一步重构使用
    getSlowSickData = async () => {
        const { patientId } = this.props;
        const data = await api.getPatientDiseaseInfo(patientId);
        console.log('datadatadata', data);
        this.setState({
            allergies: data.allergies
        })
    }
    textareaChange = (evt) => {
        this.setState({
            allergies:evt.target.value
        })
    }
    allergiesSave = debounce(async () => {
        const { patientId } = this.props;
        const { allergies } = this.state;
        const diseData = {
            chronicDiseases: [],
            homeMedicalHistories: [],
            allergies:allergies
        }
        try {
            this.setState({
                allergiesSaveLoading:true
            })
            await api.updateDiseaseInfo(patientId, diseData);
            message.success('保存成功')
            this.setState({
                allergiesEdit: true,
                allergiesSaveLoading:false
            })
        }
        catch (e) {
            this.setState({
                allergiesSaveLoading:false
            })
            message.error(e.message?e.message:'过敏史保存失败，请刷新后重试');
        }
    },300)
    render() {
        const pathname = window.location.pathname;
        const search = window.location.search;
        // 跳到用药需求 MedicationDemand
        // const url = pathname.replace('/CreateHealthRecords','/MedicationDemand') + search;
        // 跳到规律用药 RegularMedication
        const url = pathname.replace('/CreateHealthRecords','/RegularMedication') + search;
        const { diseaseInfo } = this.props;
        return (
            <div className="disease-info-edit">
                <FormItem
                    colon={false}
                    label="过敏史"
                >
                    <div className="marginR120 test">
                        <TextArea value={this.state.allergies} rows={2} onChange={(evt) => this.textareaChange(evt)} maxLength={200} placeholder="输入其他过敏药物或者过敏食物" readonly={this.state.allergiesEdit ? 'readonly' : false} className={ this.state.allergiesEdit?"noEdit":null }/>
                        <div className="allergiesRow">
                            {this.state.allergiesEdit ? <span className="allergiesSaveBtn" onClick={() => { this.setState({allergiesEdit:false})}}>编辑</span>
                                : <span className="allergiesSaveBtn" onClick={() => this.allergiesSave()}>{ this.state.allergiesSaveLoading?<Icon type="loading" />:null} 保存</span>}
                        </div>
                    </div>
                </FormItem>
            </div>
        );
    }
}
